package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javaee.common.result;
import com.example.javaee.entity.Asset;
import com.example.javaee.entity.AssetCategory;
import com.example.javaee.entity.AssetOrg;
import com.example.javaee.entity.AttackTask;
import com.example.javaee.service.AssetCategoryService;
import com.example.javaee.service.AttackTaskService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/attackTask")
public class AttackTaskController {
    @Autowired
    private AttackTaskService attackTaskService;

    //分页获取所有任务
    @GetMapping("/getTasks/{pg}")
    public result<Page> page(@PathVariable Integer pg){
        //分页构造器
        Page<AttackTask> pageInfo = new Page<>(pg,12);
        //条件构造器
        LambdaQueryWrapper<AttackTask> queryWrapper = new LambdaQueryWrapper<>();
        //分页查询
        attackTaskService.page(pageInfo,queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo,"查询成功");

    }

    //新增任务
    @PostMapping("/add")
    public result<AttackTask> add(HttpServletRequest request, @RequestBody AttackTask attackTask) {

        //根据页面提交的资产编号orgName查询数据库
        LambdaQueryWrapper<AttackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttackTask::getTaskName, attackTask.getTaskName());
        AttackTask tk = attackTaskService.getOne(queryWrapper);
        if (attackTaskService.getOne(queryWrapper) != null) {
            return result.error("资产已存在");
        }
        attackTaskService.save(attackTask);
        return result.success(null, "资产添加成功");
    }

    //通过id获取任务信息
    @GetMapping("/getTaskById/{id}")
    public result<AttackTask> getTask(@PathVariable Integer id){
        LambdaQueryWrapper<AttackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttackTask::getId, id);
        AttackTask at = attackTaskService.getOne(queryWrapper);
        System.out.printf(String.valueOf(at));
        return result.success(at,"查询成功");
    }
    //通过id删除资产
    @DeleteMapping("/delete/{id}")
    public result<AttackTask> delete(@PathVariable Integer id) {
        if(attackTaskService.removeById(id)){
            return  result.success(null,"删除成功");}
        else{
            return result.error("删除失败");
        }
    }

    //更新任务

    @PostMapping("/update")
    public result<AttackTask> update(@RequestBody(required = false) AttackTask attackTask) {
        UpdateWrapper<AttackTask> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", attackTask.getId());
        if(attackTaskService.update(attackTask, updateWrapper)){
            return result.success(null, "更新成功");
        } else {
            return result.error("更新失败");
        }
    }

    //通过joinedTesters获取任务信息
    @GetMapping("/getTaskByTesters/{joinedTesters}")
    public result<AttackTask> getTaskByTesters(@PathVariable String joinedTesters){
        LambdaQueryWrapper<AttackTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AttackTask::getId, joinedTesters);
        AttackTask at = attackTaskService.getOne(queryWrapper);
        System.out.printf(String.valueOf(at));
        return result.success(at,"查询成功");
    }

}
